package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.Hospitalization;
import com.kkb.service.HospitalizationService;
import com.kkb.vo.HospitalizationQueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("hospitals")
@ResponseBody
public class HospitalizationController {
    @Resource
    HospitalizationService hospitalizationService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    ResultVO queryAll(int index, int size, HospitalizationQueryVO hospitalizationQueryVO) {
        return new ResultVO(hospitalizationService.queryAll(index, size, hospitalizationQueryVO));
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    ResultVO queryById(@PathVariable("id") int id) {
        Hospitalization hospitalization = hospitalizationService.queryById(id);
        if (hospitalization != null)
            return new ResultVO(hospitalization);
        else
            return new ResultVO(404, "未找到数据");
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO add(Hospitalization hospitalization) {
        hospitalization.setInTime(new Date());
        hospitalization.setState(0);
        hospitalization.setPaidState(0);
        int result = hospitalizationService.add(hospitalization);
        return result == 1 ? new ResultVO("添加成功!") : new ResultVO(500, "添加失败!");
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO update(@PathVariable int id, double deposit) {
        Hospitalization hospitalization = hospitalizationService.queryById(id);
        hospitalization.setDeposit(hospitalization.getDeposit() + deposit);
        int updated = hospitalizationService.update(hospitalization);
        return updated == 1 ? new ResultVO("存款成功!") : new ResultVO(404, "存款失败!");
    }

    @RequestMapping(value = "out", method = RequestMethod.PUT)
    public ResultVO outHospital(@RequestParam("patients") List<Integer> patients) {
        int updated = hospitalizationService.outHospital(patients);
        if (updated == patients.size()) {
            return new ResultVO<>("出院成功!");
        } else {
            return new ResultVO<>("操作完成，" + (patients.size() - updated) + "位病人出院失败");
        }
    }

    @RequestMapping(value = "exit", method = RequestMethod.PUT)
    public ResultVO exitHospital(@RequestParam("patients") List<Integer> patients) {
        int updated = hospitalizationService.exitHospital(patients);
        if (updated == patients.size()) {
            return new ResultVO<>("退院成功!");
        } else {
            return new ResultVO<>("操作完成，" + (patients.size() - updated) + "位病人退院失败");
        }
    }
}
